[% setvar title Subroutine prototypes and parameters %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Subroutine prototypes and parameters</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Andy Wardley &lt;<a href='mailto:abw@kfs.org'>abw@kfs.org</a>&gt;
  Date: 7 Aug 2000
  Mailing List: <a href='mailto:perl6-language-subs@perl.org'>perl6-language-subs@perl.org</a>
  Number: 57
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Perl 6 should provide support for named subroutine prototypes.  This
should permit the use of positional and named parameters, default
values and optionally, type checking.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>When defining a sub-routine in Perl 6 it should be possible to provide
a prototype which names the variables passed and specifies their basic
types.</p>
<pre>    sub foo ($x, $y) {
        ...
    }</pre>
<p>This would be equivalent to the Perl 5 construct:</p>
<pre>    sub foo ($$) {
	my ($x, $y) = @_;
    }</pre>
<p>Here are two more examples specifying a scalar followed by an
array/hash array to gobble up all remaining arguments.</p>
<pre>    sub bar ($first, @rest) {
	...			    # my ($first, @rest) = @_;
    }

    sub baz ($first, %rest) {
	...			    # my ($first, %rest) = @_;
    }</pre>
<p>The subroutine could be called with positional parameters, as in Perl 5:</p>
<pre>    foo(10, 20);
    bar(30, 40, 50, ...);		# @rest = (40, 50, ...)
    baz(60, one =&gt; 1, two =&gt; 2, ...);	# %rest = (one =&gt; 1, two =&gt; 2, ... )</pre>
<p>or instead with named parameters.  But how exactly?  One candidate for
the calling convention would be:</p>
<pre>    # common Perl 5 form, '=&gt;' implies LHS is quoted
    foo(x =&gt; 10, y =&gt; 20);</pre>
<p>However, we should consider the fact that '=&gt;' is (currently) synonymous
for ',' and the above example would be analogous to a regular argument list
in Perl 5:</p>
<pre>    foo('x', 10, 'y', 20);</pre>
<p>This is then indistinct from calling with positional parameters (also
see the earlier hash example) and any &quot;hidden magic&quot; we add to handle
this case might serve only to confuse. A better alternative might be
to make the parameters look properly like variable assignment, as they
indeed are.</p>
<pre>    foo($x = 10, $y = 20);</pre>
<p>However, this would also be a major incompatability with Perl 5 which
would interpret the above as something similar to:</p>
<pre>    $x = 10;
    $y = 20;
    foo($x, $y);</pre>
<p>This has a dangerous and unpredictable side-effect which we would rather
avoid.  So the third and preferred candidate is:</p>
<pre>    foo(x = 10, y = 20);</pre>
<p>In Perl 5 this is a syntax error raising a 'Bareword &quot;x&quot; not
allowed...'  error message.  This is a Good Thing because it implies
there aren't (m)any existing scripts that are currently using this
construct.</p>
<p>It should be possible to use a combination of positional and named
parameters.  e.g.</p>
<pre>    sub connect ($dsn, $user, $pass) {
	...
    }

    connect('dbi:MySQL...', 'abw', 'magik');
    connect('dbi:MySQL...', user = 'abw', pass = 'magik');
    connect(dsn = 'dbi:MySQL...', user = 'abw', pass = 'magik');</pre>
<p>Ordering should be insignificant.</p>
<pre>    connect(pass = 'magik', user = 'abw', dsn = 'dbi:MySQL...');</pre>
<p>Positional parameters would be mapped onto the first prototype parameter
that isn't explicitly named elsewhere in the subroutine arguments.</p>
<pre>    connect(user = 'abw', 'dbi:MySQL...', 'magik');</pre>
<p>In this example, the first positional parameter ('dbi:MySQL...') is
mapped to $dsn, the second ('magik') is mapped to $pass, and $user is
named explicitly.  We would, of course, recommend that users keep
positional parameters at the start and named parameters at the end of
the argument list to reduce confusion.</p>
<p>Array and hash values could also be passed by name, although this may
be more difficult to parse (for perl and perl hackers alike).</p>
<pre>    sub bar ($x, @y) { ... }
    sub baz ($x, %y) { ... }

    bar(x = 10, y = (10, 20, 30));
    baz(x = 10, y = (one =&gt; 1, two =&gt; 2));</pre>
<p>RFC 9 proposes &quot;Highlander Variables&quot; in which <code>$y</code> is implicitly a reference
to <code>%y</code> or &lt;@y&gt;.  This would allow lists and hashes to be passed by reference
as named parameters to the same effect.  This is preferable to the above, (in
the author's opinion) because the different types are more clearly
disambiguated.</p>
<pre>    bar(x = 10, y = [ 10, 20, 30 ]);
    baz(x = 10, y = { one =&gt; 1, two =&gt; 2 });</pre>
<p>Default values could also be provided in subroutine prototypes.</p>
<pre>    sub wiz ($first, $second, $third = 5, $fourth = 7) {
	...
    }</pre>
<p>This would remove the need for the existing ';' prototype character
which delimits mandatory from optional arguments.  We might also
consider that this would remove the restriction that mandatory arguments
must always be specified before optional ones.  Again, this might
be something that's possible but not necessarily recommended.</p>
<pre>    sub daz ($foo = 99, $bar, $baz = 98) {
	...
    }</pre>
<p>Ideally, we would allow more complex Perl expressions to be used as
defaults.  All prototype parameters should be visible to these
expressions and they should be evaluated in order.</p>
<pre>    sub waz ($one, $two, 
             $three = add($one, $two), 
             $four  = add($three, 1)) {
	...
    }</pre>
<p>We might like to consider a pragma (<code>strict prototypes</code> for example)
which would perform sanity checks on all arguments passed, ensuring that
all mandatory parameters (i.e. that have no defaults) have defined values
and match their expected type.</p>
<pre>    sub woz ($mand, $opt = 123) {
	...
    }

    woz(10);		# OK
    woz(10, 20);	# OK
    woz();		# NOT OK: 'no value specified for $mand'
    woz(opt = 20);	# NOT OK: 'no value specified for $mand'</pre>
<p>Other prototype characters inherited from Perl 5:</p>
<pre>    mysub(&amp;x);		# expect sub-routine ref, aliased to $x
    mysub(*h);		# filehandle (see RFC 10)</pre>
<p>These would be automatically checked to ensure references of the
correct type have been passed, as per Perl 5.</p>
<p>We could extend this mechanism to give type checking against user-defined
types for those that want it.</p>
<pre>    sub blah (User $user, Project $project) {
	...
    }

    $u = User-&gt;new(...);
    $p = Project-&gt;new(...);
    
    blah($u, $p);			# OK
    blah($p, $u);			# NOT OK
    blah(user = $u, project = $p);	# OK
    blah(user = $p, project = $u);	# NOT OK
    blah(project = $p, user = $u);	# OK
    blah(project = $u, user = $p);	# NOT OK</pre>
<p>Finally, we could define Perl inbuilt functions to also accepting named
parameters.</p>
<pre>    split(/:/, limit=2);		# instead of split(/:/, $_, 2);
    splice(@data, length=7);		# 'offset' defaults to 0</pre>
<p>None of the features proposed here would be mandatory.  Extended prototypes
and associated features would be available for those that wanted to use them.
Those that didn't would be free to continue happily without them (see
RFC 10).</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>We would need to special-case the interpretation of subroutine
parameters to treat a bareword followed by '=' as a named parameter.
Note that there may be a grammar conflict if lvalue subs are supported
in Perl 6, allowing <code>foo = 10</code> to be equivalent to <code>foo(10)</code>.</p>
<p>Nothing else concrete, yet.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<pre>    RFC  9: Highlander variables
    RFC 10: Filehandles should use C&lt;*&gt; as a type prefix if typeglobs 
            are eliminated.
    RFC 28: Perl should stay Perl. </pre>
</div>
